﻿# coding: utf-8
import wx
import wx.lib.agw.foldpanelbar as fpb
import EntityModel
import Model
import Skeleton
import SkeletonData
import Mesh
import Resource

class wxPanelMesh(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id, style = wx.BORDER)
        
        self._pnl = fpb.FoldPanelBar(self, -1, wx.DefaultPosition, wx.Size(-1, -1), fpb.FPB_DEFAULT_STYLE, 0)
        self._il = wx.ImageList(IL.IMAGE_SMALL, IL.IMAGE_SMALL)
        self._il.Add(IL.GetBitmap(IL.ID_IMAGE_FOLDER_OPEN, IL.IMAGE_SMALL))
        self._il.Add(IL.GetBitmap(IL.ID_IMAGE_FOLDER_CLOSE, IL.IMAGE_SMALL))
        
        self.__CreatePanelGeneral()
        self.__CreatePanelSubMeshList()
                                
        style = fpb.CaptionBarStyle()
        style.SetCaptionStyle(fpb.CAPTIONBAR_FILLED_RECTANGLE)
        style.SetFirstColour(wx.Colour(168, 168, 168))
        style.SetSecondColour(wx.Colour(128, 128, 128))
        self._pnl.ApplyCaptionStyleAll(style)
                                            
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self._pnl, 1, wx.EXPAND|wx.ALL, border = 10)
        self.SetSizer(sizer)
        
        self.UpdateUI()
        
    def __CreatePanelGeneral(self):     
        panelGeneral = self._pnl.AddFoldPanel(TD.GENERAL_MESH_INFO, collapsed = False, foldIcons = self._il)
        
        self._stVertexCount = wx.StaticText(panelGeneral)
        self._stFaceCount = wx.StaticText(panelGeneral)
        self._stSubMeshCount = wx.StaticText(panelGeneral)
        self._stBoneCount = wx.StaticText(panelGeneral)
        self._stFrameCount = wx.StaticText(panelGeneral)
        
        self._pnl.AddFoldPanelWindow(panelGeneral, self._stVertexCount, fpb.FPB_ALIGN_WIDTH, 5, 5, 5)
        self._pnl.AddFoldPanelWindow(panelGeneral, self._stFaceCount, fpb.FPB_ALIGN_WIDTH, 5, 5, 5)
        self._pnl.AddFoldPanelWindow(panelGeneral, self._stSubMeshCount, fpb.FPB_ALIGN_WIDTH, 5, 5, 5)
        self._pnl.AddFoldPanelWindow(panelGeneral, self._stBoneCount, fpb.FPB_ALIGN_WIDTH, 5, 5, 5)
        self._pnl.AddFoldPanelWindow(panelGeneral, self._stFrameCount, fpb.FPB_ALIGN_WIDTH, 5, 5, 5)
        self._pnl.AddFoldPanelWindow(panelGeneral, wx.StaticText(panelGeneral), fpb.FPB_ALIGN_WIDTH, 5, 5, 5)

    def __CreatePanelSubMeshList(self):
        panelSubMesh = self._pnl.AddFoldPanel(TD.SUB_MESH_LIST, collapsed = False, foldIcons = self._il)
        self._listSubMesh = wx.ListCtrl(panelSubMesh, -1, size = (-1, 200), style = wx.LC_REPORT)
        self._listSubMesh.InsertColumn(0, TD.SUB_MESH_FILE_PATH)
        self._pnl.AddFoldPanelWindow(panelSubMesh, self._listSubMesh, fpb.FPB_ALIGN_WIDTH, 5, 5, 5) 
            
    def UpdateUI(self):
        vertexCount = -1
        faceCount = -1
        subMeshCount = -1
        boneCount = -1
        frameCount = -1
        subMeshList = []

        if MODEL_EDITOR._modelEntityHandle:
            # General information
            coreModelHandle = EntityModel.GetCoreModel(MODEL_EDITOR._modelEntityHandle)
            vertexCount = Model.GetVertexCount(coreModelHandle)
            faceCount = Model.GetFaceCount(coreModelHandle)
            subMeshCount = Model.GetSubMeshCount(coreModelHandle)
            skeletonHandle = Model.GetCurrentAnimation(coreModelHandle)
            if skeletonHandle:
            	skeletonData = Skeleton.GetSkeletonData(skeletonHandle)
                boneCount = SkeletonData.GetBoneCount(skeletonData)
                frameCount = SkeletonData.GetFrameCount(skeletonData)
                
            # Sub mesh list
            for i in xrange(0, subMeshCount):
                subMesh = Model.GetSubMeshByIdx(coreModelHandle, i)
                fileName = Resource.GetResourceName(subMesh)
                subMeshList.append(fileName.decode('gbk'))          
                
        self._stVertexCount.SetLabel(TD.VERTEX_COUNT % vertexCount)
        self._stFaceCount.SetLabel(TD.FACE_COUNT % faceCount)
        self._stSubMeshCount.SetLabel(TD.SUB_MESH_COUNT % subMeshCount)
        self._stBoneCount.SetLabel(TD.BONE_COUNT % boneCount)
        self._stFrameCount.SetLabel(TD.FRAME_COUNT % frameCount)    
        
        self._listSubMesh.DeleteAllItems()
        for file_path in subMeshList:
            self._listSubMesh.InsertStringItem(self._listSubMesh.GetItemCount(), file_path)